# -*- encoding=utf8 -*-
        
__author__ = "Bryan.Chen"
__desc__=""
        
from airtest.core.api import *
        
auto_setup(__file__)
        
from poco.drivers.std import StdPoco
poco = StdPoco()
        
using("../FlowTest.air")
import FlowTest

using("SideMenu_Open.air")
import SideMenu_Open
        
class SideMenu_Recommend(FlowTest.BaseTestCase):
    def __init__(self):
        print("%s __init__" % (__file__))
        
    def runTest(self, args):
        print("%s runTest" % (__file__))
        # 打开左侧菜单
        isOpenSideMenu = SideMenu_Open.runTest({})
        if not isOpenSideMenu:
            return False

        # 获取左侧菜单节点
        bottomPanel = poco("bottom_panel")
        if not bottomPanel.exists():
            return False
        # 获取绑定按钮
        btnBind = bottomPanel.offspring("btn_Bind")
        if not btnBind.exists():
            # 截图保存
            agentID = bottomPanel.offspring("txt_recommonderID").get_text()
            assert_equal(True, True, "已经绑定过代理，代理ID为[%s]" % agentID)
            poco.click([0.7, 0.5])
            return True

        agentNum = []
        if "agentNum" in args:
            agentNum = args["agentNum"]

        assert_equal(len(agentNum), 6, "默认指定代理ID[%s]长度正确" % "".join(agentNum))

        # 点击绑定按钮
        self.clickButton(btnBind)
        # 等待绑定弹框弹出
        try:
            poco("BindAgentView").wait_for_appearance()
        except:
            assert(0, 1, "绑定代理弹框显示正常")
            return False
        # 获取代理ID输入框
        bindAgentView = poco("BindAgentView")
        if not bindAgentView.exists():
            return False
        # 输入代理ID
        for i in range(0, len(agentNum)):
            btnName = "button_number_%s" % agentNum[i]
            btnNum = bindAgentView.offspring(btnName)
            if btnNum.exists():
                self.clickButton(btnNum)
        # 获取绑定代理确认弹框
        bgTips = bindAgentView.offspring("tips_bg")
        if not bgTips.exists():
            assert(0, 1, "绑定代理确认弹框显示正常")
            return False
        # 获取并点击确认按钮
        btnOK = bgTips.offspring("ok_btn_2")
        if btnOK.exists():
            self.clickButton(btnOK)

        # 获取提示信息
        toastView = poco("ToastView")        
        if toastView.exists():
            txtContent = toastView.offspring("text_content").get_text()
            assert_equal(txtContent, "绑定成功", "成功绑定代理")

        poco.click([0.7, 0.5])

        return True
        
_instance = SideMenu_Recommend()
runTest = _instance.runTest